package root.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import root.utils.StringHelper;

/**
 * 用户管理service
 *
 * @author 吕梁山
 * @date 2018/9/21
 */
public class UserService extends JdbcTemplate {

    /**
     * 分页获取用户列表
     */
    public Page<Record> getUserPage(String user_email, int pageNumber, int pageSize) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select u.*,(select count(0) from t_article_comment c where c.user_id = u.id) comment_num from t_user u where 1 = 1");
        if(StringHelper.isNotEmpty(user_email)){
            stringBuilder.append(" and user_email = '" + user_email + "'");
        }
        stringBuilder.append(" order by u.create_date desc");
        return Db.paginate(pageNumber, pageSize, new SqlPara().setSql(stringBuilder.toString()));
    }

    /**
     * 查询单个用户信息
     */
    public Record getUserInfo(String user_id) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select * from t_user where id = " + user_id);
        return Db.findFirst(stringBuilder.toString());
    }

    public boolean saveUser(Record record) {
        return Db.update("t_user","id", record);
    }
}
